package com.yanqu.road.server.manger.activity.jieshi;

import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.jieshi.JieShiUserData;
import com.yanqu.road.entity.activity.jieshi.config.JieShiConfig;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.logic.activity.jieshi.logic.JieShiMapLogic;
import com.yanqu.road.logic.bussiness.activity.JieShiActivityBusiness;
import com.yanqu.road.pb.activity.JieShiProto;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.jieshi.JieShiModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.pb.JieShiPb;

import java.util.List;
import java.util.Map;

/**
 * 灯火街市
 */
public class JieShiMgr extends TempMgr {

    private static JieShiConfig config = null;

    public static JieShiConfig getConfig() {
        return config;
    }

    public static int JIESHI_MAX_NICK_NAME_LENGTH = 12;

    public static int JIESHI_MIN_NICK_NAME_LENGTH = 4;

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return false;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }

    public static void reloadActivityData() {
        getLogger().info("reload denghuojieshi activity start");
        List<ActivityInfo> list = NormalActivityMgr.getOpenActivityInfoList(eActivityType.JieShi.getValue());
        if (list.isEmpty()) {
            config = null;
            getLogger().info("reload denghuojieshi end, no activity in show time.");
            return;
        }
        ActivityInfo activityInfo = list.get(0);

        loadConfigFromDb(activityInfo);

        // 地图相关数据加载到内存
        if (config != null) {
            int chooseMapId = config.getLIGHTMARKET_MAP_ID_CHOOSE();
            config.setJieShiMap(JieShiMapLogic.getJieShiMap(chooseMapId));
            config.setJieShiMapPath(JieShiMapLogic.getJieShiMapPath(chooseMapId));
            config.setJieShiNpcPos(JieShiMapLogic.getJieShiNpcPos(chooseMapId));
            config.setMapWidth(JieShiMapLogic.getJieShiMapWidth(chooseMapId));
        }

        getLogger().info("reload denghuojieshi end, activityId {}.", config.getActivityInfo().getActivityId());

        for (GamePlayer player : GamePlayerMgr.getAllOnlinePlayer()) {
            player.getModule(JieShiModule.class).initSystem();
        }
    }

    /**
     * 数据库加载配置
     */
    private static void loadConfigFromDb(ActivityInfo activityInfo) {
        Map<String, ActivityConfig> activityConfigMap = NormalActivityMgr.getActivityConfigMap(activityInfo.getActivityId());
        JieShiConfig tmpConfig = new JieShiConfig();
        tmpConfig.initConfig(activityConfigMap);
        tmpConfig.setActivityInfo(activityInfo);
        // 其他配置
        tmpConfig.setMainLvConfigMap(JieShiActivityBusiness.getLightMarketMainLvConfigList(activityInfo.getActivityId()));
        tmpConfig.setShopConfigMap(JieShiActivityBusiness.getLightMarketShopConfigList(activityInfo.getActivityId()));
        tmpConfig.setPatronConfigMap(JieShiActivityBusiness.getLightMarketPatronConfigList(activityInfo.getActivityId()));
        tmpConfig.setRhythmConfigMap(JieShiActivityBusiness.getLightMarketRhythmConfigList(activityInfo.getActivityId()));
        tmpConfig.setEventConfigMap(JieShiActivityBusiness.getLightMarketEventConfigList(activityInfo.getActivityId()));
        // 初始化配置
        config = tmpConfig;
    }

    /**
     * 是否是人偶道具
     */
    public static boolean isDollItem(int goodsId) {
        if (config != null && ActivityMgr.activityInShowTime(config.getActivityInfo())) {
            return goodsId == config.getLIGHTMARKET_DOY_DEFAULT_ITEM_ID() ||
                    goodsId == config.getLIGHTMARKET_DOY_WULI_ITEM_ID() ||
                    goodsId == config.getLIGHTMARKET_DOY_JIAOZI_ITEM_ID();
        }
        return false;
    }

    /**
     * 拜访其他玩家街市
     */
    public static JieShiProto.JieShiVisitOtherRespMsg.Builder visitOther(long userId) {
        JieShiProto.JieShiVisitOtherRespMsg.Builder respMsg = JieShiProto.JieShiVisitOtherRespMsg.newBuilder();
        GamePlayer onlinePlayer = GamePlayerMgr.getOnlinePlayer(userId);
        JieShiUserData userData;
        if (onlinePlayer != null) {
            userData = onlinePlayer.getModule(JieShiModule.class).getUserData();
        } else {
            userData = JieShiActivityBusiness.getJieShiUserData(config.getActivityInfo().getActivityId(), userId);
        }
        JieShiProto.JieShiUserDataTemp.Builder userDataTemp = JieShiPb.parseUserDataTemp(userData);
        if (userDataTemp != null) {
            respMsg.setUserData(userDataTemp);
        }
        respMsg.setRet(0);
        return respMsg;
    }
}
